package com.example.a410.mybt;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import static com.example.a410.mybt.MainActivity.mMainActivity;

public class KeyboardActivity extends AppCompatActivity {
    private Switch mEditSwtich;
    private Button mKey1Btn;
    private Button mKey2Btn;
    private Button mKey3Btn;

    private Button mKey4Btn;
    private Button mKey5Btn;
    private Button mKey6Btn;

    private Button mKey7Btn;
    private Button mKey8Btn;
    private Button mKey9Btn;

    private Button mKey10Btn;
    private Button mKey11Btn;
    private Button mKey12Btn;
    SharedPreferences preferences;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_keyboard);

        // keyboard.xml 用于自定义键盘数据的存储，保存上一次存储的数据
        preferences = getSharedPreferences("keyboard", Context.MODE_WORLD_READABLE);
        //实例化Editor对象,写数据
        editor = preferences.edit();

        mEditSwtich = (Switch) findViewById(R.id.edit_keyboard_switch);
        mKey1Btn = (Button) findViewById(R.id.key_btn1);
        mKey2Btn = (Button) findViewById(R.id.key_btn2);
        mKey3Btn = (Button) findViewById(R.id.key_btn3);

        mKey4Btn = (Button) findViewById(R.id.key_btn4);
        mKey5Btn = (Button) findViewById(R.id.key_btn5);
        mKey6Btn = (Button) findViewById(R.id.key_btn6);

        mKey7Btn = (Button) findViewById(R.id.key_btn7);
        mKey8Btn = (Button) findViewById(R.id.key_btn8);
        mKey9Btn = (Button) findViewById(R.id.key_btn9);

        mKey10Btn = (Button) findViewById(R.id.key_btn10);
        mKey11Btn = (Button) findViewById(R.id.key_btn11);
        mKey12Btn = (Button) findViewById(R.id.key_btn12);

        mEditSwtich.setChecked(false);
        mKey1Btn.setText(preferences.getString("key1Text", null));
        mKey2Btn.setText(preferences.getString("key2Text", null));
        mKey3Btn.setText(preferences.getString("key3Text", null));

        mKey4Btn.setText(preferences.getString("key4Text", null));
        mKey5Btn.setText(preferences.getString("key5Text", null));
        mKey6Btn.setText(preferences.getString("key6Text", null));

        mKey7Btn.setText(preferences.getString("key7Text", null));
        mKey8Btn.setText(preferences.getString("key8Text", null));
        mKey9Btn.setText(preferences.getString("key9Text", null));

        mKey10Btn.setText(preferences.getString("key10Text", null));
        mKey11Btn.setText(preferences.getString("key11Text", null));
        mKey12Btn.setText(preferences.getString("key12Text", null));

        mEditSwtich.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mEditSwtich.setChecked(true);
                    Toast.makeText(KeyboardActivity.this, "打开编辑模式", Toast.LENGTH_SHORT).show();
                } else {
                    mEditSwtich.setChecked(false);
                    Toast.makeText(KeyboardActivity.this, "关闭编辑模式", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mKey1Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key1Text", "key1OnClickMsg", "key1ReleaseMsg", mKey1Btn);
                }
            }
        });

        mKey1Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key1OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key1ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮2
        mKey2Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key2Text", "key2OnClickMsg", "key2ReleaseMsg", mKey2Btn);
                }
            }
        });

        mKey2Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key2OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key2ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮3
        mKey3Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key3Text", "key3OnClickMsg", "key3ReleaseMsg", mKey3Btn);
                }
            }
        });

        mKey3Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            //mKey3Btn.setBackgroundColor(Color.parseColor("#00BFFF"));
                            mMainActivity.sendData("" + preferences.getString("key3OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            //mKey3Btn.setBackgroundColor(Color.parseColor("#F0F8FF"));
                            mMainActivity.sendData("" + preferences.getString("key3ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮4
        mKey4Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key4Text", "key4OnClickMsg", "key4ReleaseMsg", mKey4Btn);
                }
            }
        });

        mKey4Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key4OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key4ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮5
        mKey5Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key5Text", "key5OnClickMsg", "key5ReleaseMsg", mKey5Btn);
                }
            }
        });

        mKey5Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key5OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key5ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮6
        mKey6Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key6Text", "key6OnClickMsg", "key6ReleaseMsg", mKey6Btn);
                }
            }
        });

        mKey6Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key6OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key6ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮7
        mKey7Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key7Text", "key7OnClickMsg", "key7ReleaseMsg", mKey7Btn);
                }
            }
        });

        mKey7Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key7OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key7ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮8
        mKey8Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key8Text", "key8OnClickMsg", "key8ReleaseMsg", mKey8Btn);
                }
            }
        });

        mKey8Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key8OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key8ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮9
        mKey9Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key9Text", "key9OnClickMsg", "key9ReleaseMsg", mKey9Btn);
                }
            }
        });

        mKey9Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key9OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key9ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮10
        mKey10Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key10Text", "key10OnClickMsg", "key10ReleaseMsg", mKey10Btn);
                }
            }
        });

        mKey10Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key10OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key10ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮11
        mKey11Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key11Text", "key11OnClickMsg", "key11ReleaseMsg", mKey11Btn);
                }
            }
        });

        mKey11Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key11OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key11ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });

        // 按钮12
        mKey12Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditSwtich.isChecked()) {
                    showAddItemDialog(KeyboardActivity.this, "key12Text", "key12OnClickMsg", "key12ReleaseMsg", mKey12Btn);
                }
            }
        });

        mKey12Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!mEditSwtich.isChecked() && mMainActivity.getBlueToothIsConnented()) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            mMainActivity.sendData("" + preferences.getString("key12OnClickMsg", null));
                            return true; // if you want to handle the touch event
                        }
                        case MotionEvent.ACTION_UP: {
                            mMainActivity.sendData("" + preferences.getString("key12ReleaseMsg", null));
                            return true; // if you want to handle the touch event
                        }
                    }
                } else {
                    if (!mEditSwtich.isChecked()) {
                        // 提示设备未连接
                        promptUnconnect();
                    }
                }
                return false;
            }
        });
    }



    public void showAddItemDialog(Context c, String skeyText, String skeyOnClickMsg,
                                  String skeyReleaseMsg, final Button button) {
        Context context = c;
        final String keyText = skeyText;
        final String keyOnClickMsg  = skeyOnClickMsg;
        final String keyReleaseMsg  = skeyReleaseMsg;
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);

        final TextView btnPromptText = new TextView(context);
        btnPromptText.setText("按钮文本");
        layout.addView(btnPromptText);
        final EditText btnText = new EditText(context);
        btnText.setHint("编辑按钮文本");
        if (preferences.getString(keyText, null) != null)
            btnText.setText("" + preferences.getString(keyText, null));
        layout.addView(btnText); // Notice this is an add method

        final TextView promptOnClick = new TextView(context);
        promptOnClick.setText("点击按钮");
        layout.addView(promptOnClick);
        final EditText onClickMsg = new EditText(context);
        onClickMsg.setHint("消息");
        if (preferences.getString(keyOnClickMsg, null) != null)
            onClickMsg.setText("" + preferences.getString(keyOnClickMsg, null));
        layout.addView(onClickMsg); // Another add method

        final TextView promptRelease = new TextView(context);
        promptRelease.setText("释放按钮");
        layout.addView(promptRelease);
        final EditText releaseMsg = new EditText(context);
        releaseMsg.setHint("消息");
        if (preferences.getString(keyReleaseMsg, null) != null)
            releaseMsg.setText("" + preferences.getString(keyReleaseMsg, null));
        layout.addView(releaseMsg); // Another add method

        AlertDialog.Builder dialog = new AlertDialog.Builder(c);
        dialog.setTitle("按钮编辑器");
        dialog.setView(layout); // Again this is a set method, not add
        dialog.setCancelable(false);
        dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 响应确认
                editor.putString(keyText, ""+btnText.getText());
                editor.commit();
                editor.putString(keyOnClickMsg, ""+onClickMsg.getText());
                editor.commit();
                editor.putString(keyReleaseMsg, ""+releaseMsg.getText());
                editor.commit();
                button.setText(btnText.getText());
            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 响应取消
            }
        });
        dialog.show();
    }

    public void promptUnconnect() {
        AlertDialog dialog = new AlertDialog.Builder(KeyboardActivity.this)
                .setTitle("设备未连接！")
                .setMessage("发送信息需要连接目标设备。如需继续编辑，请忽略。")
                .setPositiveButton("我知道了", null)
                .setNegativeButton("忽略", null)
                .create();
        dialog.show();
    }
}
//    private void showAddItemDialog(Context c) {
//        AlertDialog.Builder dialog = new AlertDialog.Builder(c);
//        dialog.setTitle("按钮编辑器");
//        dialog.setMessage("按钮文本");
//        final EditText btnText = new EditText(c);
//        dialog.setView(btnText);
//        dialog.setMessage("点击按钮");
//        final EditText clickBtn = new EditText(c);
//        dialog.setView(clickBtn);
//        dialog.setMessage("释放按钮");
//        final EditText releaseBtn = new EditText(c);
//        dialog.setView(releaseBtn);
//        dialog.setCancelable(false);
//        dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                // 响应确认
//            }
//        });
//        dialog.setPositiveButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                // 响应取消
//            }
//        });
//        dialog.show();
//    }
//}
